/*
 * ActualEESTraderApi.h
 *
 *  Created on: 2021��8��15��
 *      Author: root
 */

#ifndef ACTUALEESTRADERAPI_H_
#define ACTUALEESTRADERAPI_H_
#include <cstdlib>
#include <cstdio>
#include <dlfcn.h>
#include <string>
#include <unistd.h>
#include "EesTraderApi.h"
#include "future_tick.h"
#include "DataEngine.h"
#include "spinlock.h"
#include "EESDataStruct.h"
#include <sys/epoll.h>
#include <sys/socket.h>
#include <netinet/in.h>
using namespace std;
typedef void*		T_DLL_HANDLE;



class ActualEESTraderApi:public EESTraderEvent // @suppress("Symbol is not resolved")
{
private:

	T_DLL_HANDLE				m_handle;				///< EES����API���?
	funcDestroyEESTraderApi		m_distoryFun;			///< EES����API��̬�����ٺ��� // @suppress("Type cannot be resolved")

	string						m_tradeServerIp;		///< ����ͨ��IP��ַ
	unsigned short				m_tradeServerPort;		///< ����ͨ���˿�
	unsigned short				m_tradeServerUDPPort;	///< UDP�������˿�
	string						m_queryServerIp;		///< ��ѯͨ��IP��ַ
	unsigned short				m_queryServerPort;		///< ��ѯͨ���˿�
	string						m_udpLocalIp;			///< UDP����IP
	unsigned short				m_udpLocalPort;			///< UDP���ض˿�
	string						m_logonId;				///< ��¼���׷������?
	string						m_logonPwd;				///< ��¼���׷�������
	
	string                  m_appid;					//
	string						m_authCode;			///< ����M

	int                     m_userId;
	int							m_logonStatus;			///< 0-��δ��¼; 1-���������ڵ�¼; 2-��¼�ɹ�; 3-��¼ʧ�ܣ������Ѿ��Ͽ�����

    vector<EES_SymbolField> contractList;
	DataEngine* p_engine= nullptr;

	bool             isBind;
	thread* p_order= nullptr;
    char last_EnterTime[9] = {0};
	unsigned int sumVol=0;
    unordered_map<EES_ClientToken,string> tokenContractMap;
	int shfeSessions [4]={12,13,14,15};
    int ineSessions [2]={31,32};
    double				m_InitialBp=0;
    double              m_CurrentBp=0;
    double              m_Fee=0;
    EES_EnterOrigOrderField origOrderField;
    string						m_account;

    Event                       tmpEvent;
    MY_SOCKET					m_socketUdp=-1;

    struct sockaddr_in			m_remote_addr;
public:
    			///< �����ʺ�
	EESTraderApi*				m_tradeApi;				///< EES����API�ӿ� // @suppress("Type cannot be resolved")
	ActualEESTraderApi(map<string,string> params,DataEngine* p_engine);
	
	~ActualEESTraderApi() override;

	//void InsertOrder(char exchangeId,const string& contractCode,double price, int vol, char flag,int strategyId);

    //void InsertOrder(char exchangeId,const string& contractCode,double price,unsigned int vol, char flag,EES_ClientToken token) ;

	//void  InsertOrderFak(char exchangeId,const string& contractCode,double price, int vol, char flag,int strategyId);

	//void CancelOrder(long long int);

	//void startEESOrderThread();

	RESULT ChangePassword(const char* oldPwd, const char* newPwd);

    //void startEESOrderThread();

    void startUdpOrderThread();

private:
	/// \brief ��ʼ��
	bool Init();
	/// \brief �ر�
	void Close();

	/// \brief ��ʱ����
	void ApiSleep(int nMilliSeconds);
	/// \brief ��ͣ
	void Pause();
	/// \brief ����EES����API�Ķ�̬��
	bool LoadEESTrader();
	/// \brief �ͷ�EES����API�Ķ�̬��
	void UnloadEESTrader();


	/// \brief Linux�汾���ؽ���API�Ķ�̬��
	bool Linux_LoadEESTrader();
	/// \brief Linux�汾�ͷŽ���API�Ķ�̬��
	void Linux_UnloadEESTrader();

	/// \brief ��ʼ��������������
	void InputParam(map<string,string>& params);

	/// \brief ��¼
	void Logon();
	/// \brief �µ�
	/**
	 * d�ǿ�ƽ�ͷ���
	 */

	/// \brief ����

	/// \brief �����������¼�
	void OnConnection(ERR_NO errNo, const char* pErrStr) override;
	/// \brief	�����������Ͽ������յ�������?
	void OnDisConnection(ERR_NO errNo, const char* pErrStr) override;
	/// \brief	��¼��Ϣ�Ļص�
	void OnUserLogon(EES_LogonResponse* pLogon) override;

    void OnUserLogon(EES_LogonResponse* pLogon, MY_SOCKET m_sock) override;
	/// \brief	�µ���REM���ܵ��¼�
	void OnOrderAccept(EES_OrderAcceptField* pAccept) override;
	/// \brief	�µ����г����ܵ��¼�
	void OnOrderMarketAccept(EES_OrderMarketAcceptField* pAccept) override;
	///	\brief	�µ���REM�ܾ����¼�
	void OnOrderReject(EES_OrderRejectField* pReject) override;
	///	\brief	�µ����г��ܾ����¼�
	void OnOrderMarketReject(EES_OrderMarketRejectField* pReject) override;
	///	\brief	�����ɽ�����Ϣ�¼�
	void OnOrderExecution(EES_OrderExecutionField* pExec) override;
	///	\brief	�����ɹ������¼�
	void OnOrderCxled(EES_OrderCxled* pCxled) override;
	///	\brief	�������ܾ�����Ϣ�¼�
	void OnCxlOrderReject(EES_CxlOrderRej* pReject) override;

	void OnSymbolStatusReport(EES_SymbolStatus* pSymbolStatus) override;

	void OnQuerySymbolStatus(EES_SymbolStatus* pSymbolStatus, bool bFinish) override;

	void OnQueryMarketSession(EES_ExchangeMarketSession* pMarketSession, bool bFinish) override;

    //unordered_map<EES_ClientToken,string>& getTokenMap();
	///	查询订单的返回事件
	
		/// \brief	查询订单信息时候的回调，这里面也可能包含不是当前用户下的订单
		/// \param  pAccount                 帐户ID
		/// \param  pQueryOrder	             查询订单的结构
		/// \param  bFinish	                 如果没有传输完成，这个值是 false，如果完成了，那个这个值为 true
		/// \remark 如果碰到 bFinish == true，那么是传输结束，并且 pQueryOrder值无效。
		/// \return void 
	
	void OnQueryTradeOrder(const char* pAccount, EES_QueryAccountOrder* pQueryOrder, bool bFinish) override;
    /// \brief	查询订单信息时候的回调，这里面也可能包含不是当前用户下的订单成交
    /// \param  pAccount                        帐户ID
    /// \param  pQueryOrderExec	                查询订单成交的结构
    /// \param  bFinish	                        如果没有传输完成，这个值是false，如果完成了，那个这个值为 true
    /// \remark 如果碰到 bFinish == true，那么是传输结束，并且pQueryOrderExec值无效。
    /// \return void
    void OnQueryTradeOrderExec(const char* pAccount, EES_QueryOrderExecution* pQueryOrderExec, bool bFinish) override;

    void OnQueryUserAccount(EES_AccountInfo * pAccoutnInfo, bool bFinish) override;

	void OnQuerySymbol(EES_SymbolField* pSymbol, bool bFinish) override;

    /// 触发篮子回报
    /// \brief 表示触发篮子回报
    /// \param  pAccept	                   篮子触发以后的消息体
    /// \return void
    void OnOrigOrderReport(EES_OrigOrderReportField* pAccept) override;
};




#endif /* ACTUALEESTRADERAPI_H_ */
